import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:text_gradiate/text_gradiate.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/utils.dart';
import 'package:vape_xe/values/values.dart';
import 'package:vape_xe/widgets/widgets.dart';

import 'logic.dart';

class WithdrawView extends GetView<WithdrawController> {
  const WithdrawView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new_rounded),
          onPressed: () => Get.back(),
        ),
        title: Text('Withdraw'.tr),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          20.hGap,
          Padding(
            padding: EdgeInsets.only(left: 29.w),
            child: Text('Withdraw amount'.tr, style: const TextStyle(color: AppColors.secondaryText)),
          ),
          16.hGap,
          InnerContainerWidget(
            margin: EdgeInsets.only(left: 29.w, right: 32.w),
            child: Row(
              children: [
                16.vGap,
                Text(
                  ConfigStore.to.currency,
                  style: TextStyle(fontFamily: 'Montserrat', fontWeight: FontWeight.bold, fontSize: 12.sp),
                ),
                4.vGap,
                Expanded(
                  child: TextField(
                    controller: controller.moneyController,
                    keyboardType: TextInputType.number,
                    focusNode: controller.focusNode,
                    inputFormatters: [
                      FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')),
                      LengthLimitingTextInputFormatter(10), // 可选，限制总长度
                    ],
                    decoration: const InputDecoration(border: InputBorder.none),
                    style: TextStyle(color: Colors.white, fontSize: 20.sp, fontWeight: FontWeight.bold),
                  ),
                ),
                16.vGap,
              ],
            ),
          ),
          8.hGap,
          Padding(
            padding: EdgeInsets.only(left: 29.w),
            child: Obx(
              () => TextGradiate(
                text: Text('${'Maximum withdraw amount'.tr}:${ConfigStore.to.currency} ${controller.balance.value}', style: TextStyle(fontSize: 12.sp)),
                colors: const [Color(0xff6EDEF7), Color(0xff3351EF)],
                gradientType: GradientType.linear,
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                tileMode: TileMode.clamp,
              ),
            ),
          ),
          56.hGap,
          Padding(
            padding: EdgeInsets.only(left: 29.w),
            child: Text('Withdraw methods'.tr, style: const TextStyle(color: AppColors.secondaryText)),
          ),
          16.hGap,
          Obx(
            () => ListView.builder(
              itemCount: controller.withdrawTypeList.length,
              shrinkWrap: true, // 防止 ListView 自身滚动
              physics: const NeverScrollableScrollPhysics(), // 禁止 ListView 自身滚动
              itemBuilder: (context, index) => GestureDetector(
                onTap: () => controller.onTypeChange(index),
                child: Container(
                  height: 60.h,
                  margin: EdgeInsets.only(left: 29.w, right: 32.w, bottom: 12.h),
                  decoration: BoxDecoration(
                      color: controller.withdrawTypeList[index]['isSelected'] ? Colors.white10 : Colors.transparent,
                      borderRadius: 12.radius,
                      border: controller.withdrawTypeList[index]['isSelected']
                          ? null
                          : Border.all(color: Colors.white10, width: 1.w)),
                  child: Row(
                    children: [
                      16.vGap,
                      Image.asset(controller.withdrawTypeList[index]['icon'], width: 28.w, height: 28.w),
                      16.vGap,
                      Expanded(
                        child: Text(
                          controller.withdrawTypeList[index]['name'],
                          style: TextStyle(fontSize: 16.sp, fontFamily: 'Montserrat'),
                        ),
                      ),
                      Image.asset(
                          controller.withdrawTypeList[index]['isSelected']
                              ? 'assets/images/ic_sel_pre.webp'
                              : 'assets/images/ic_sel_nor.webp',
                          width: 20.w,
                          height: 20.w),
                      16.vGap,
                    ],
                  ),
                ),
              ),
            ),
          ),
          GestureDetector(
            onTap: () => controller.nextStep(),
            child: Container(
              height: 52.h,
              margin: EdgeInsets.all(32.w),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: 16.radius,
                gradient: const LinearGradient(colors: [Color(0xffF5B946), Color(0xffF58546)]),
              ),
              child: Text(
                'Withdraw'.tr,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontFamily: 'Montserrat',
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
